<?php
/**
 * 顶部图片管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-11-08
 */
namespace Admin\Controller; 

class DrawController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db_userRole  =D("UserRole");
        $this->db_drawData  =D("drawData");
    }
    
    /*
     * 抽奖人员次数列表
     * 
     * @return #
     */
    public function drawIndex(){
        if(I("request.user")){
            $_REQUEST['user_id']=$this->getIds(I("request.user"),"name","User");
        }
        //获取当前用户的权限id
        $roleId =$this->db_userRole->where(array("u_id"=>session("authId")))->find();        
        if($roleId['role_id']<4){
            $this->assign("roleSign",1);
        }
        $this->index("DrawNum");
    }
    
    /*
     * show前置操作
     * 
     * @return #
     */
    public function _before_show(){
        if(I("request.sign",'','code')==2 && I("request.roleSign",'','code')!=1){
            $this->error("无权进行此操作");
        }
    }
    
    /*
     * 获奖统计
     * 
     * @return #
     */
    public function drawRecord(){
        if(I("request.user")){
            $_REQUEST['user_id']        =$this->getIds(I("request.user"),"name","User");
        }
        if(I("request.is_go_user")){
            $_REQUEST['is_go_userid']   =$this->getIds(I("request.is_go_user"),'name','User');
        }
        if(I("request.draw")){
            $_REQUEST['draw_id']        =$this->getIds(I("request.draw"),"name","Draw");
        }
        $this->index("DrawData",'draw_grade',TRUE);
    }
    
    /*
     * 为获奖用户派奖
     * 
     * @return #
     */
    public function is_go(){
        $map['is_go']       =1;
        $map['is_go_userid']=session("authId");
        $map['is_go_time']  =time();
        $map['id']          =I("request.id",'','code');
        if($this->db_drawData->save($map)){
            $this->success("派奖成功");
        }else{
            $this->error("派奖失败");
        }
    }
    
    /*
     * 删除前做判断，禁止删除3个月内的数据
     * 
     * @return #
     */
    public function _before_delete(){
        if(!I("request.db")){
            $model  =D(CONTROLLER_NAME);
        }else{
            $model  =D(I("request.db","","code"));  
        }
        if (!empty ($model)) {
            $pk = $model->getPk();
            $id = $_REQUEST [$pk];
            if(!is_array($id)){                
                $id = explode(",",code($id));
            }
            if (isset($id)) {
                $condition  =array($pk => array('in', implode(',', $id)));
                $deleteList =$model->where($condition)->select(array("field"=>"add_time"));                
                $add_time   =array();                
                foreach($deleteList as $vo){
                    $add_time[] =$vo['add_time'];
                }
                if(!empty($add_time[0])){
                    foreach($add_time as $v){
                        if(((time()-strtotime($v))/3600/24/30)<3){
                            $this->error("不允许清理三个月以内的数据");
                            break;
                        }
                    }
                }                
            }
        }
    }
}